package com.example.user.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.aliyun.vod.upload.common.MD5Util;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.edu.exceptionhandler.HeartEduException;
import com.example.user.entity.UcenterMember;
import com.example.user.entity.voco.LoginVo;
import com.example.user.entity.voco.RegisterVo;
import com.example.user.mapper.UcenterMemberMapper;
import com.example.user.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.user.utils.JwtUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-13
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        if(StringUtils.isEmpty(loginVo)||StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw new HeartEduException(20001,"出现异常");
        }

        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(queryWrapper);
        if(null==ucenterMember){
            throw new HeartEduException(20001,"未查到数据");
        }
        if(!MD5Util.md5(password).equals(ucenterMember.getPassword())){
            throw new HeartEduException(20001,"未找到密码匹配的用户");
        }
        if(ucenterMember.getIsDisabled()){
            throw new HeartEduException(20001,"该用户被禁用");
        }

        String token = JwtUtils.getJwtToken(ucenterMember.getId(),ucenterMember.getNickname());
        return token;
    }

    @Override
    public Boolean register(RegisterVo registerVo) {

        String mobile = registerVo.getMobile();
        String code = registerVo.getCode();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        if(StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(nickname) ||
                StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(code)) {
            throw new HeartEduException(20001,"error");
        }
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!redisCode.equals(code)){
            throw new HeartEduException(20001,"error");
        }
        UcenterMember ucenterMember = new UcenterMember();
        BeanUtils.copyProperties(registerVo,ucenterMember);
        ucenterMember.setPassword(MD5Util.md5(password));
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        int insert = baseMapper.insert(ucenterMember);

        return insert>0;
    }

    @Override
    public Integer getRegistersByDay(String day) {
        Integer registerNum = baseMapper.getRegisterByDay(day);
        return registerNum;
    }
}
